package web.controller;

import com.alibaba.fastjson.JSONObject;
import common.model.BaseModel;
import module.model.UserModel;
import module.service.UserService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import web.security.vo.UserDetailVo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.beans.PropertyEditorSupport;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 项目名称：sdheryFramework-project
 * 类名称：
 * 类描述：
 * 创建人：Administrator
 * 创建时间：2015/5/4 17:35
 * 修改人：Administrator
 * 修改时间：2015/5/4 17:35
 * 修改备注：
 */
public abstract class BaseController<T extends BaseModel, ID extends Serializable> {
    @Autowired(required=false)
    protected HttpServletRequest request;
    @Autowired(required=false)
    protected HttpServletResponse response;
    @Autowired(required=false)
    protected HttpSession session;

    protected abstract  String getPrefix();

    protected  final String UPDATE=getPrefix()+"form";
    protected  final String ADD=getPrefix()+"form";
    protected  final String LIST=getPrefix()+"list";
    @Autowired
    private UserService userService;

    public void copyProperties(Object target, Object source) {
        try {
            BeanUtils.copyProperties(target, source);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public void setSuccess(ModelMap model) {
        model.put("success", true);
    }

    public void setFailure(ModelMap model) {
        model.put("success", false);
    }

    public void setErrMsg(ModelMap model, String errMsg) {
        model.put("errMsg", errMsg);
    }

    @InitBinder
    public void InitBinder(WebDataBinder dataBinder) {
        dataBinder.registerCustomEditor(Date.class,
                new PropertyEditorSupport() {
                    public void setAsText(String value) {
                        try {
                            setValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                                    .parse(value));
                        } catch (Exception e) {
                            try {
                                setValue(new SimpleDateFormat("yyyy-MM-dd HH:mm")
                                        .parse(value));
                            } catch (ParseException e1) {
                                try{
                                    setValue(new SimpleDateFormat("yyyy-MM-dd")
                                            .parseObject(value));
                                }catch (ParseException e2) {
                                    setValue(null);
                                }
                            }
                        }
                    }

                    public String getAsText() {
                        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                                .format((Date) getValue());
                    }

                });

        dataBinder.registerCustomEditor(Long.class,
                new PropertyEditorSupport() {
                    public void setAsText(String value) {
                        try {
                            setValue(Long.parseLong(value));
                        } catch (Exception e) {
                            setValue(null);
                        }
                    }

                    public String getAsText() {
                        return (String) getValue();
                    }
                });

        dataBinder.registerCustomEditor(long.class,
                new PropertyEditorSupport() {
                    public void setAsText(String value) {
                        try {
                            setValue(Long.parseLong(value));
                        } catch (Exception e) {
                            setValue(0L);
                        }
                    }

                    public String getAsText() {
                        return (String) getValue();
                    }
                });

        dataBinder.registerCustomEditor(Integer.class,
                new PropertyEditorSupport() {
                    public void setAsText(String value) {
                        try {
                            setValue(new Double(value).intValue());
                        } catch (Exception e) {
                            setValue(null);
                        }
                    }

                    public String getAsText() {
                        return (String) getValue();
                    }
                });

        dataBinder.registerCustomEditor(int.class,
                new PropertyEditorSupport() {
                    public void setAsText(String value) {
                        try {
                            setValue(new Double(value).intValue());
                        } catch (Exception e) {
                            setValue(0);
                        }
                    }

                    public String getAsText() {
                        return (String) getValue();
                    }
                });
    }



    @RequestMapping(value = "list",method = RequestMethod.GET)
    public String list(){
        return LIST;
    }



    @RequestMapping(value = "add",method = RequestMethod.GET)
    public String form(ModelMap map){
        return ADD;
    }

    public UserModel getLoginAdmin() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) {
            return null;
        }
        Object principal = authentication.getPrincipal();
        if (principal == null || !(principal instanceof UserDetailVo)) {
            return null;
        } else {
            return userService.findByAdminLoginId(((UserDetailVo) principal).getUsername());
        }
    }

    public Long getCurAdminId(){
        return getLoginAdmin().getUserId();
    }

}
